/**
 * 基础菜单 JS BOT
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>响应配置</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="websocket：">
        <el-select size="small" v-model="formInline.responseType" clearable placeholder="请选择">
          <el-option v-for="item in websocketData" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="指令：">
        <el-input size="small" v-model="formInline.command" placeholder="输入指令"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column prop="websocketName" label="websocket">
      </el-table-column>
      <el-table-column prop="command" label="指令">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <el-table size="small" :data="scope.row.commandList" style="width: 100%;">
              <el-table-column align="center" prop="command" label="指令">
              </el-table-column>
              <el-table-column align="center" prop="commandTypeName" label="指令类型">
              </el-table-column>
              <el-table-column align="center" prop="at" label="是否需要@" width="120">
                <template slot-scope="c">
                  <el-switch
                    disabled="true"
                    v-model="c.row.at"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0">
                  </el-switch>
                </template>
              </el-table-column>
            </el-table>
            <div slot="reference" class="name-wrapper">
              {{ scope.row.command }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="responseTypeName" label="响应方式">
      </el-table-column>
      <el-table-column prop="content" label="响应内容">
      </el-table-column>
      <el-table-column prop="at" label="是否需要@">
        <template slot-scope="scope">
          <el-switch
            disabled="true"
            v-model="scope.row.at"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="是否启用">
        <template slot-scope="scope">
          <el-switch
            disabled="true"
            v-model="scope.row.enable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注">
      </el-table-column>
      <el-table-column align="center" label="操作" width="160">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteResponse(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="50%" top="8vh" @click="closeDialog">
      <el-form :model="editForm" :rules="rules" ref="editForm" class="demo-form-inline" v-loading="editLoading">
        <el-col :span="8">
          <el-form-item label="websocket" label-width="90px" prop="websocketId">
            <el-select size="small" v-model="editForm.websocketId" placeholder="请选择">
              <el-option v-for="item in websocketData" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="响应方式" label-width="90px" prop="responseClass">
            <el-select size="small" v-model="editForm.responseClass" placeholder="请选择">
              <el-option v-for="item in responseClassList" :key="item.key" :label="item.name" :value="item.key"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注" label-width="90px" prop="remark">
            <el-input size="small" v-model="editForm.remark" placeholder="请输入响应内容">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="响应内容" label-width="90px" prop="content">
            <el-input size="small" v-model="editForm.content" type="textarea"
              :autosize="{ minRows: 3}" auto-complete="off" placeholder="请输入响应内容">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否需要@" label-width="90px" prop="at">
            <el-switch
              v-model="editForm.at"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否启用" label-width="90px" prop="enable">
            <el-switch
              v-model="editForm.enable"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="" label-width="90px">
            <div>&nbsp;</div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-table border :data="editForm.commandList" max-height="180">
            <el-table-column align="center" prop="command" label="指令">
                <template scope="scope">
                    <el-form-item :prop="'commandList.' + scope.$index + '.command'" :rules="rules.command">
                        <el-input size="small" v-model="scope.row.command" placeholder="请输入" clearable>
                        </el-input>
                    </el-form-item>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="commandType" label="指令类型">
                <template scope="scope">
                    <el-form-item :prop="'commandList.' + scope.$index + '.commandType'" :rules="rules.commandType">
                        <el-select size="small" v-model="scope.row.commandType" placeholder="请选择">
                          <el-option v-for="item in dictData['botCommandType']" :key="item.dictCode" :label="item.dictName" :value="item.dictCode"></el-option>
                        </el-select>
                    </el-form-item>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="at" label="是否需要@">
                <template scope="scope">
                    <el-form-item :prop="'commandList.' + scope.$index + '.at'" :rules="rules.at">
                        <el-switch
                          v-model="scope.row.at"
                          active-color="#13ce66"
                          inactive-color="#ff4949"
                          active-value="1"
                          inactive-value="0">
                        </el-switch>
                    </el-form-item>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="left" width="150" fixed="right">
                <template slot-scope="scope">
                    <el-button type="text" style="color: #67C23A; margin-bottom: 22px;"
                        @click="addRow(scope.$index, scope.row)" icon="el-icon-plus">新增
                    </el-button>
                    <el-button type="text" style="color: #F56C6C; margin-bottom: 22px;" v-if="scope.$index > 0"
                        @click="delRow(scope.$index, scope.row)"
                        icon="el-icon-delete">删除
                    </el-button>
                </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getResponseList, addResponse, deleteResponse, getAllResponseClass, getWebsocketList } from '../../api/botMG'
import { getDictByType, upload, downloadUrl } from '../../api/userMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      downloadUrl,
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {},
      // rules表单验证
      rules: {
        websocketId: [{ required: true, message: '请选择websocket', trigger: 'blur' }],
        responseType: [{ required: true, message: '请选择响应方式', trigger: 'blur' }],
        parsingMode: [{ required: true, message: '请选择解析方式', trigger: 'blur' }],
        content: [{ required: true, message: '请输入响应内容', trigger: 'blur' }],
        command: [{ required: true, message: '请输入指令', trigger: 'blur' }]
      },
      formInline: {
        pageNum: 1,
        pageSize: 10
      },
      websocketData: [],
      responseClassList: [],
      dictData: {},
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
    this.getWebsocketData()
    this.getDictByType('botCommandType')
    this.getDictByType('botParsingMode')
    this.getAllResponseClass()
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    getWebsocketData() {
      getWebsocketList({})
      .then(res => {
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.websocketData = res.data
        }
      })
      .catch(err => {
        this.$message.error('websocket列表加载失败，请稍后再试！')
      })
    },
    getDictByType(type) {
      getDictByType(type)
      .then(res => {
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.dictData[type] = res.data
        }
      })
      .catch(err => {
        this.$message.error('字典['+type+']加载失败，请稍后再试！')
      })
    },
    getAllResponseClass() {
      getAllResponseClass()
      .then(res => {
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.responseClassList = res.data
        }
      })
      .catch(err => {
        this.$message.error('字典['+type+']加载失败，请稍后再试！')
      })
    },
    // 获取列表
    getdata(parameter) {
      this.loading = true
      getResponseList(parameter)
      .then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          res.data.forEach(response => {
            response['command'] = response.commandList.map(item => item.command).join(',')
          })
          this.listData = res.data
          // 分页赋值
          this.pageparm.currentPage = this.formInline.pageNum
          this.pageparm.pageSize = this.formInline.pageSize
          this.pageparm.total = res.total
        }
      })
      .catch(err => {
        this.loading = false
        this.$message.error('响应列表加载失败，请稍后再试！')
      })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm = row
      } else {
        this.title = '添加'
        this.editForm = {
          websocketId: this.websocketData[0].id,
          responseType: '1',
          parsingMode: '1',
          content: '',
          at: '1',
          enable: '1',
          remark: '',
          commandList: [{
            command: '',
            commandType: '1',
            at: '0'
          }]
        }
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          addResponse(this.editForm)
            .then(res => {
              this.editFormVisible = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除
    deleteResponse(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true
          deleteResponse(row.id)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.loading = false
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.loading = false
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 新增
    addRow(index, row) {
      this.editForm.commandList.push({
        command: '',
        commandType: '1',
        at: '0'
      })
    },
    // 删除
    delRow(index, row) {
      this.editForm.commandList.splice(index, 1)
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
      this.editForm = {}
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
</style>

 
 